package cn.sumpu.app.guoguojie.web.mvc.family;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import cn.sumpu.app.guoguojie.biz.FamilyBiz;
import cn.sumpu.app.guoguojie.biz.FamilyTagBiz;
import cn.sumpu.app.guoguojie.biz.FriendBiz;
import cn.sumpu.app.guoguojie.biz.VisitorBiz;
import cn.sumpu.app.guoguojie.constant.GroupClassify;
import cn.sumpu.app.guoguojie.domain.Family;
import cn.sumpu.app.guoguojie.domain.FamilyTag;
import cn.sumpu.app.guoguojie.domain.Group;
import cn.sumpu.app.guoguojie.domain.Member;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;
import cn.sumpu.app.guoguojie.util.Common;
import cn.sumpu.app.guoguojie.domain.Ext.FriendFamily;
import cn.sumpu.app.guoguojie.domain.Ext.PageModel;

@Controller
public class FriendController {

	@Autowired
	private FriendBiz friendBiz;
	@Autowired
	private VisitorBiz visitorBiz;
	@Autowired
	private FamilyBiz familyBiz;
	@Autowired
	private FamilyTagBiz familyTagBiz;

	/**
	 * 查找好友页面
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/enterSearchFriend.htm", method = RequestMethod.GET)
	public String enterSearchFriend(HttpServletRequest request) {
		return "friend/searchFriend";
	}

	/**
	 * 按条件查找好友页面
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/searchFriendCondition.htm", method = RequestMethod.GET)
	public String searchFriendCondition(HttpServletRequest request) {
		return "friend/searchFriendCondition";
	}

	/**
	 * 查找好友
	 * 
	 * @param request
	 * @return
	 * @throws AppBusinessException
	 */
	@RequestMapping(value = "/searchFriend.htm")
	public String searchFriend(HttpServletRequest request) {
		try{
			Member member = (Member) request.getSession().getAttribute("member");
			List<FamilyTag> familyTagList = familyTagBiz.getFamilyTagByDoorplate(member.getFamily());
			request.setAttribute("familyTagList", familyTagList);
			String email = request.getParameter("email");
			String doorplate = request.getParameter("doorplate");
			String tag = request.getParameter("tag");
			String page = request.getParameter("page"); 
			if(Common.empty(page)){
				page="1";
			}
			
			FriendFamily m = null;
			List<FriendFamily> ffList = null;
			PageModel pageModel = new PageModel();
			if (!Common.empty(email)) {
				m = friendBiz.searchFriendEmail(member.getFamily(),email);
				if(m!=null){
					ffList = new ArrayList<FriendFamily>();
					ffList.add(m);
					pageModel.setCurrentPage(Integer.parseInt(page));
					pageModel.setTotalPage(1);
					pageModel.setTotalRecord(1);
					pageModel.setList(ffList);
				}else{
					pageModel.setCurrentPage(Integer.parseInt(page));
					pageModel.setTotalPage(0);
					pageModel.setTotalRecord(0);
				}
				
				request.setAttribute("pageModel", pageModel);
				return "friend/searchFriendResult";
			}
			if (!Common.empty(doorplate)) {
				m = friendBiz.searchFriendDoorplate(member.getFamily(),doorplate);
				if(m!=null){
					ffList = new ArrayList<FriendFamily>();
					ffList.add(m);
					pageModel.setCurrentPage(Integer.parseInt(page));
					pageModel.setTotalPage(1);
					pageModel.setTotalRecord(1);
					pageModel.setList(ffList);
				}else{
					pageModel.setCurrentPage(Integer.parseInt(page));
					pageModel.setTotalPage(0);
					pageModel.setTotalRecord(0);
				}
				request.setAttribute("pageModel", pageModel);
				return "friend/searchFriendResult";
			}
			if (!Common.empty(tag)) {
				pageModel = friendBiz.searchFriendByTag(tag, member.getFamily(), Integer.parseInt(page));
				request.setAttribute("pageModel", pageModel);
				request.setAttribute("tag", tag);
				return "friend/searchFriendResult";
			}

			String fnickName = request.getParameter("fnickName");
			String username = request.getParameter("username");
			String sex = request.getParameter("sex");
			String birthday = request.getParameter("birthday");
			
			String province = request.getParameter("province");
			String city = request.getParameter("city");
			String district = request.getParameter("district");
			String birthPro = request.getParameter("birthPro");
			String birthCity = request.getParameter("birthCity");
			String birthDic = request.getParameter("birthDic");
			
			String babyName = request.getParameter("babyName");
			
			if("0000-00-00".equals(birthday)){
				birthday = null;
			}
			pageModel = friendBiz.searchFriendCondition(member.getFamily(), username, fnickName, sex, birthday, birthPro, birthCity, birthDic, province, city, district, babyName,Integer.parseInt(page) );
			request.setAttribute("pageModel", pageModel);
			request.setAttribute("fnickName", fnickName);
			request.setAttribute("username", username);
			request.setAttribute("sex", sex);
			request.setAttribute("birthday", birthday);
			request.setAttribute("province", province);
			request.setAttribute("city", city);
			request.setAttribute("district", district);
			request.setAttribute("birthPro", birthPro);
			request.setAttribute("birthCity", birthCity);
			request.setAttribute("birthDic", birthDic);
			request.setAttribute("babyName", babyName);
			return "friend/searchFriendResult";
		}catch(Exception e){
			e.printStackTrace();
			return "common/error";
		}
	}

	/**
	 * 添加好友弹出框
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/friendAdd.htm", method = RequestMethod.GET)
	public String friendAdd(String doorplate, HttpServletRequest request) {
		try {

			Family family = familyBiz.getFamilyByDoor(doorplate);
			request.setAttribute("family", family);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return "friend/friendAdd";
	}

	/**
	 * 添加好友发送消息
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/friendAddSave.htm", method = RequestMethod.POST)
	public String friendAddSave(String doorplate, String content,
			HttpServletRequest request, HttpServletResponse response) {
		Member member = (Member) request.getSession().getAttribute("member");
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter pw = null;
		try {
			pw = response.getWriter();
			friendBiz.friendAddSave(member, doorplate, content);
			pw.print("success");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 我的好友主页面
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/friend.htm")
	public String friend(HttpServletRequest request) {
		Member m = (Member) request.getSession().getAttribute("member");
		String page = request.getParameter("page");
		String group = request.getParameter("group");
		if (Common.empty(page)) {
			page = "1";
		}
		try {
			List<Group> groupList = friendBiz.getGroupList(m.getFamily());
			if (groupList == null || groupList.size() < 1) {
				groupList = new ArrayList<Group>();
				Group g = friendBiz.createGroup(m.getFamily(), "朋友", GroupClassify.FRIEND.getValue(),null);
				Group g2 = friendBiz.createGroup(m.getFamily(), "亲戚",  GroupClassify.RELATIVE.getValue(),null);
				groupList.add(g);
				groupList.add(g2);
			}

			PageModel pageModel = friendBiz.getMyFriendsFamily(m.getFamily(),group,Integer.parseInt(page));
			request.setAttribute("groupList", groupList);
			request.setAttribute("friendModel", pageModel);
			request.setAttribute("group", group);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "friend/friend";
	}

	/**
	 * 我的好友管理
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/friendManage.htm")
	public String friendManage(HttpServletRequest request) {
		Member m = (Member) request.getSession().getAttribute("member");
		String page = request.getParameter("page");
		String group = request.getParameter("group");
		if (Common.empty(page)) {
			page = "1";
		}
		try {
			List<Group> groupList = friendBiz.getGroupList(m.getFamily());
			PageModel pageModel = friendBiz.getMyFriendsFamily(m.getFamily(),group,
					Common.empty(page) ? 1 : Integer.parseInt(page));
			request.setAttribute("friendModel", pageModel);
			request.setAttribute("groupList", groupList);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "friend/friendManage";
	}

	/**
	 * 删除好友
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/friendDelete.htm")
	public String friendDelete(String friend, HttpServletRequest request) {
		Member m = (Member) request.getSession().getAttribute("member");
		try {
			friendBiz.deleteFriend(friend, m.getFamily());
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return friend(request);
	}

	/**
	 * 选择分组
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/selectGroup.htm")
	public String selectGroup(String friendId, String group,
			HttpServletRequest request, HttpServletResponse response) {
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter pw = null;
		try {
			pw = response.getWriter();
			friendBiz.updateFriendGroup(friendId, group);
			pw.print("success");
		} catch (IOException e) {
			e.printStackTrace();
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 查找好友分类
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/friendGroup.htm")
	public String friendGroup(Integer type,Integer index,String friendId,HttpServletRequest request, HttpServletResponse response) {
		Member memeber = (Member) request.getSession().getAttribute("member");
		try {
			List<Group> groupList = friendBiz.getGroupList(memeber.getFamily());
			request.setAttribute("groupList", groupList);
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		request.setAttribute("index", index);
		request.setAttribute("type", type);
		request.setAttribute("friendId", friendId);
		return "friend/friendGroup";
	}
	
	/**
	 * 增加好友分类
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/addFriendGroup.htm")
	public String addFriendGroup(String groupName,Integer type,Integer index,String friendId,HttpServletRequest request, HttpServletResponse response) {
		Member memeber = (Member) request.getSession().getAttribute("member");
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter pw = null;
		try {
			pw = response.getWriter();
			Group group = friendBiz.addFriendGroup(memeber.getFamily(), groupName, friendId);
			pw.print(JSONObject.fromObject(group).toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 删除好友分类
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/deleteFriendGroup.htm")
	public String deleteFriendGroup(String groupId,Integer type,Integer index,String friendId,HttpServletRequest request, HttpServletResponse response) {
		Member member = (Member) request.getSession().getAttribute("member");
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter pw = null;
		try {
			pw = response.getWriter();
			friendBiz.deleteGroup(groupId, member.getFamily(), friendId);
			pw.print("success");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 屏蔽好友动态
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/friendShield.htm")
	public String friendShield(boolean shield,String friendId,HttpServletRequest request, HttpServletResponse response) {
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter pw = null;
		try {
			pw = response.getWriter();
			friendBiz.updateFriendShield(shield, friendId);
			pw.print("success");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 访问脚印主页面
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/friendVisitor.htm")
	public String friendVisitor(HttpServletRequest request) {
		Member m = (Member) request.getSession().getAttribute("member");
		String page = request.getParameter("page");
		if (Common.empty(page)) {
			page = "1";
		}
		try {
			PageModel pageModel = visitorBiz.visitorMember(m.getFamily(),
					Integer.parseInt(page));
			request.setAttribute("visitorModel", pageModel);
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return "friend/friendVisitor";
	}

	/**
	 * 访问脚印谁来过我家
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/friendVisitorMember.htm")
	public String friendVisitorMember(HttpServletRequest request) {
		Member m = (Member) request.getSession().getAttribute("member");
		String page = request.getParameter("page");
		if (Common.empty(page)) {
			page = "1";
		}
		try {
			PageModel pageModel = visitorBiz.visitorMember(m.getFamily(),
					Integer.parseInt(page));
			request.setAttribute("visitorModel", pageModel);
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return "friend/friendVisitorMember";
	}

	/**
	 * 访问脚印我去过谁家
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/friendVisitorFamily.htm")
	public String friendVisitorFamily(HttpServletRequest request) {
		Member m = (Member) request.getSession().getAttribute("member");
		String page = request.getParameter("page");
		if (Common.empty(page)) {
			page = "1";
		}
		try {
			PageModel pageModel = visitorBiz.visitorFamily(m.getIdentifier(),
					Integer.parseInt(page));
			request.setAttribute("visitorModel", pageModel);
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return "friend/friendVisitorFamily";
	}
	
	/**
	 * 可能认识的街坊
	 * 
	 * @param request
	 * @return
	 * @throws AppBusinessException
	 */
	@RequestMapping(value = "/maybeFriends.htm")
	public String maybeFriends(HttpServletRequest request)
			throws AppBusinessException {
		Member member = (Member) request.getSession().getAttribute("member");
		String page = request.getParameter("page");
		if(Common.empty(page)){
			page="1";
		}
		
		PageModel pageModel = new PageModel();
		
		pageModel = friendBiz.maybeFriends(member.getFamily(), Integer.parseInt(page));
		request.setAttribute("pageModel", pageModel);
		
		return "friend/maybeFriends";
	}
	
	//进入他家邻居页面
	@RequestMapping(value = "/taFriend.htm")
	public String taFriend(HttpServletRequest request, String family) {
		
		String page = request.getParameter("page");
		String group = request.getParameter("group");
		if (Common.empty(page)) {
			page = "1";
		}
		try {
			List<Group> groupList = friendBiz.getGroupList(family);
			if (groupList == null || groupList.size() < 1) {
				groupList = new ArrayList<Group>();
				Group g = friendBiz.createGroup(family, "朋友", GroupClassify.FRIEND.getValue(),null);
				Group g2 = friendBiz.createGroup(family, "亲戚",  GroupClassify.RELATIVE.getValue(),null);
				groupList.add(g);
				groupList.add(g2);
			}

			PageModel pageModel = friendBiz.getMyFriendsFamily(family,group,Integer.parseInt(page));
			request.setAttribute("groupList", groupList);
			request.setAttribute("friendModel", pageModel);
			request.setAttribute("group", group);
			request.setAttribute("family", family);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "friend/taFriend";
	}
	
	//进入他家的访问脚印
	@RequestMapping(value = "/taFriendVisitor.htm")
	public String taFriendVisitor(HttpServletRequest request, String family) {
		String page = request.getParameter("page");
		if (Common.empty(page)) {
			page = "1";
		}
		try {
			PageModel pageModel = visitorBiz.visitorMember(family, Integer.parseInt(page));
			request.setAttribute("visitorModel", pageModel);
			request.setAttribute("family", family);
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return "friend/taFriendVisitor";
	}
	
	//他取看过谁的家
	@RequestMapping(value = "/taFriendVisitorFamily.htm")
	public String taFriendVisitorFamily(HttpServletRequest request, String family) {
		String page = request.getParameter("page");
		if (Common.empty(page)) {
			page = "1";
		}
		try {
			PageModel pageModel = visitorBiz.taVisitorFamily(family, Integer.parseInt(page));
			request.setAttribute("visitorModel", pageModel);
			request.setAttribute("family", family);
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return "friend/taFriendVisitorFamily";
	}

}
